/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "ui按钮.h"
#include "UI/UI.h"


static void f_按钮Draw(C_Widget* self, S_2D画布* 画布) {
	C_按钮& box = *static_cast<C_按钮*>(self);

	box.m_背景 = f_vg_genConvex((*self), *画布, E_填充模式::e_填充面, E_图层混合模式::e_Normal);
	box.m_背景2 = f_vg_genConvex((*self), *画布, E_填充模式::e_填充面, E_图层混合模式::e_Normal);
	box.m_标签 = f_vg_genString((*self), *画布, E_填充模式::e_填充面, E_图层混合模式::e_Normal, u"雅黑");
	box.m_图标 = f_vg_genIco((*self), *画布, E_图层混合模式::e_Normal, E_填充模式::e_填充面);
}

static void f_按钮变换修改(C_Widget* self) {
	C_按钮& box = *static_cast<C_按钮*>(self);

	S_2DConvexRect rect[4];
	rect[0].offset = {};
	rect[0].size = self->m_Size;
	rect[0].radius = { S_UI主题::ui_面板圆角半径 * 1.5f, S_UI主题::ui_面板圆角半径 * 1.5f };
	rect[0].AA_type = { f_PackData4X8(box.m_颜色A), 0 };

	S_GPU内存块* 形状[4];
	形状[0] = &((*self).m_UIctx->m_Ctx.m_几何图形->m_平面圆角矩形);

	std::vector<S_RGBA8UI> 颜色(4);
	颜色[0] = {255,0,255,255};

	float32 图标大小 = box.m_Size.y * 0.7f;
	//uint32 icoUV = f_vg_get图标UV(self->m_画布, box.m_图标, box.m_ICO);
	//S_2D图标 图标 = f_vg_2D图标({ box.m_Size.y * 0.5f, box.m_Size.y*0.5f }, icoUV, S_UI主题::uic_单色图标, 图标大小);
	for (uint32 i = 1; i < 4; ++i) {
		形状[i] = 形状[0];
		颜色[i] = 颜色[0];
		rect[i] = rect[0];
	}


	f_vg_tranform(*box.m_背景, {});
	f_vg_tranform(*box.m_背景2, {});
	//f_vg_text_tranform(self->m_画布, box.m_标签, {});
	//f_vg_ico_tranform(self->m_画布, box.m_图标, {});

	//f_vg_setElementRect(*box.m_背景, 1, &box.m_图元形状, &rect);
	//f_vg_setIco(box.m_画布, box.m_图标, 1, &图标);
	f_vg_drawConvex(*box.m_背景, 4, 形状, rect);
	f_vg_drawConvex(*box.m_背景2, 4, 形状, rect);
	f_vg_color(*box.m_背景, 4, 颜色.data());
	f_vg_color(*box.m_背景2, 4, 颜色.data());


	if (box.m_Size.x > box.m_Size.y * 1.2) {
		std::u16string txt = box.m_自定义属性.m_Name;
		if (box.m_自定义属性.m_Type) {
			txt = box.m_自定义属性.m_Name;
		}
		else {
			txt = box.m_WName;
		}
	}
	f_vg_drawString(*box.m_标签, u"标题字符", {{}, {26,26}}, S_UI主题::uic_文字颜色, E_对齐方式::e_据中对齐, {S_UI主题::ui_文字间隔, E_方向::e_横向} );


	std::vector<vec2>			位置(2);
	std::vector<std::u16string> 图标(2);
	std::vector<vec2> 大小(2);
	图标[0] = u"树";
	颜色[0] = {255,255,255,255};
	位置[0] = {2,2};
	大小[0] = {100,100};
	图标[1] = u"树";
	颜色[1] = {255,255,255,255};
	位置[1] = {2,2};
	大小[1] = {20,20};
	f_vg_drawIco(*box.m_图标, 1, 图标.data());
	f_vg_tranform(*box.m_图标, 1, 位置.data(), 大小.data());
	f_vg_color(*box.m_图标, 1, 颜色.data());

}


static E_事件是否传递 f_按钮单击(C_Widget* self, const E_鼠标按钮& 按钮) {
	C_按钮* but = static_cast<C_按钮*>(self);
	switch (but->m_自定义属性.m_Type) {
		case E_值类型::e_Type_Operator: {
			((fp_PropUpdate)but->m_自定义属性.m_Value)(but->m_自定义属性);
			break;
		}
		case E_值类型::e_Type_ObjectOps: {
			S_Operator* ops = (S_Operator*)but->m_自定义属性.m_Value;
			ops->m_Func(ops->m_Obj);

			if (but->m_自定义属性.m_UI && but->m_自定义属性.m_UI->m_Update) but->m_自定义属性.m_UI->m_Update(but->m_自定义属性);
			break;
		}
		default:
			break;
	}

	return E_事件是否传递::e_事件传递_终止;
}


static E_事件是否传递 on_按钮鼠标按键按下(C_Widget* self, const E_鼠标按钮& 按钮) {
	C_按钮* box = static_cast<C_按钮*>(self);

	box->m_颜色A = S_UI主题::uic_按钮高亮;
	box->m_更新绘制属性 = true;
	return E_事件是否传递::e_事件传递_终止;
}

static E_事件是否传递 on_按钮鼠标按键放开(C_Widget* self, const E_鼠标按钮& 按钮) {
	C_按钮* box = static_cast<C_按钮*>(self);

	box->m_颜色A = S_UI主题::uic_按钮焦点;
	box->m_更新绘制属性 = true;
	return E_事件是否传递::e_事件传递_终止;
}

static E_事件是否传递 on_按钮部件鼠标移入(C_Widget* self, const S_鼠标& 鼠标) {
	if (self->mf_当前部件属性修改) {
		self->mf_当前部件属性修改(self, E_UI绘制调整部分::e_进入);
	}
	C_按钮* box = static_cast<C_按钮*>(self);
	
	box->m_颜色A = S_UI主题::uic_按钮焦点;
	
	box->m_更新绘制属性 = true;
	return E_事件是否传递::e_事件传递_终止;
}
static E_事件是否传递 on_按钮部件鼠标移出(C_Widget* self, const S_鼠标& 鼠标) {
	if (self->mf_当前部件属性修改) {
		self->mf_当前部件属性修改(self, E_UI绘制调整部分::e_退出);
	}
	C_按钮* box = static_cast<C_按钮*>(self);
	
	box->m_颜色A = S_UI主题::uic_按钮背景;
	
	box->m_更新绘制属性 = true;
	return E_事件是否传递::e_事件传递_终止;
}

C_按钮::C_按钮(S_UI渲染环境* ctx, const std::u16string& 标题) : C_Widget(ctx) {
	m_WName = 标题;
	mf_鼠标点击 = f_按钮单击;
	
	mf_鼠标进入 = on_按钮部件鼠标移入;
	mf_鼠标离开 = on_按钮部件鼠标移出;
	mf_鼠标按键按下 = on_按钮鼠标按键按下;
	mf_鼠标按键放开 = on_按钮鼠标按键放开;
	
	
	m_ColorA.m_颜色 = S_UI主题::uic_按钮背景;
	m_颜色A = S_UI主题::uic_按钮背景;
	m_颜色B = S_UI主题::uic_按钮;
	m_颜色C = S_UI主题::uic_按钮高亮;
	
	m_图元形状 = &m_UIctx->m_Ctx.m_几何图形->m_平面圆角矩形;

	m_绘制标题 = true;
	m_绘制图标 = false;
	f_setSize({ 22,22 });

	//f_widget_添加绘制画布(this);
}

C_按钮::~C_按钮() {
}

void C_按钮::f_bind属性(S_Props& p) {
	C_Widget::f_bind属性(p);
	m_WName = p.m_Name;
}

C_Widget* f_ui_创建按钮(S_UI渲染环境* ctx) {
	auto* 按钮 = new C_按钮(ctx);
	return 按钮;
}

void f_ui_bind按钮操作符(C_Widget* self, S_Props& op) {
	C_按钮* but = dynamic_cast<C_按钮*>(self);

	if (but) {
		//but->m_Op = op;
		//but->m_WName = op.m_Name;
	}
}




static E_事件是否传递 f_开关单击(C_Widget* self, const E_鼠标按钮& 按钮) {
	C_开关& box = *dynamic_cast<C_开关*>(self);
	if (box.m_自定义属性.m_Type == E_值类型::e_Type_Bool) {
		bool& val = f_prop_Bool(box.m_自定义属性);

		val = !val;
		box.m_值 = val;

		box.m_自定义属性.m_风格属性->m_ICO;

		box.m_自定义属性.m_UI->m_Update(box.m_自定义属性);
	}
	else {
		box.m_值 = !box.m_值;
	}

	if (box.m_值) {
		box.m_ICO = box.m_ICO_A;
	}
	else {
		box.m_ICO = box.m_ICO_B;
	}
	
	f_widget_更新渲染层(self);
	return E_事件是否传递::e_事件传递_终止;
}

C_开关::C_开关(S_UI渲染环境& ctx) : C_Widget(&ctx) {
	mf_鼠标点击 = f_开关单击;

	m_颜色A = S_UI主题::uic_值编辑框背景;
	m_颜色B = S_UI主题::uic_值编辑框边;
}

C_开关::~C_开关() {
	
}

void C_开关::f_bind属性(S_Props& p) {
	C_Widget::f_bind属性(p);

	bool val = f_prop_Bool(p);
	if (val) {
		m_ICO = m_ICO_A;
	}
	else {
		m_ICO = m_ICO_B;
	}
}

C_Widget* f_ui_创建开关(S_UI渲染环境& ctx) {
	C_开关* box = new C_开关(ctx);
	return box;
}

void f_ui_置开关图标(C_Widget* self, const std::vector<std::u16string> ico) {
	C_开关* box = dynamic_cast<C_开关*>(self);
	box->m_ICO_A = ico[0];
	box->m_ICO_B = ico[1];
	box->mf_鼠标点击(self, {});
}

void f_ui_切换开关状态(C_Widget* self) {
	C_开关& box = *dynamic_cast<C_开关*>(self);
	box.mf_鼠标点击(self, {});
}









static void on_标签Draw(C_Widget* self, S_2D画布* 画布) {
	C_标签* box = static_cast<C_标签*>(self);
	//box->m_背景绘制.m_属性 = f_vg_gen属性(画布);
	//
	//S_凸边图元 图元 = {};
	//图元.属性 = f_vg_get属性指针(画布, box->m_背景绘制.m_属性);
	//box->m_背景绘制.f_Draw(画布, 图元, E_笔刷类型::e_单色填充);
	//
	//auto& 字体 = f_surface_get字体(*self->m_UIctx, S_UI主题::ui_默认字体);
	//box->m_字符绘制.f_Draw(画布, &字体, box->m_背景绘制.m_属性);
}

static void on_标签变换修改(C_Widget* self) {
	C_标签* box = static_cast<C_标签*>(self);

	mat3X2 Mat;
	Mat.row0 = {};
	Mat.row1 = box->m_Size;
	Mat.row2 = { S_UI主题::ui_面板圆角半径, 0 };

	//box->m_背景绘制.f_alloc(1);
	//box->m_背景绘制.f_set单色填充(0, Mat, self->m_颜色A, self->m_图元);
	//
	////return;
	//box->m_字符绘制.f_alloc(1, 32);
	//box->m_字符绘制.f_绘制文本({ Mat.row0 , Mat.row1 }, S_UI主题::uic_文字颜色, 0, box->m_标签文本, E_对齐方式::e_据中对齐);
	
}

C_标签::C_标签(S_UI渲染环境& ctx) : C_Widget(&ctx) {
	mf_绘制 = on_标签Draw;
	mf_变换 = on_标签变换修改;

	m_颜色A = S_UI主题::uic_按钮背景;
	m_颜色B = S_UI主题::uic_按钮;

	m_图元形状 = &m_UIctx->m_Ctx.m_几何图形->m_平面圆角矩形;
}

C_标签::~C_标签() {
}



C_Widget* f_ui_创建标签(S_UI渲染环境& ctx) {
	return new C_标签(ctx);
}

void f_ui_绑定标签属性(C_Widget* self, S_Props& prop) {
	C_标签* label = static_cast<C_标签*>(self);
	label->m_Value = prop;
	label->m_标签文本 = f_prop_Str(prop);
}





C_工具条::C_工具条(S_UI渲染环境& ctx) : C_Widget(&ctx) {
	mf_布局 = f_ui_Layout_工具条;
	m_布局方式 = E_布局方式::e_HBOX布局;
}

C_工具条::~C_工具条() {
	
}

C_Widget* f_ui_创建工具条(S_UI渲染环境& ctx, std::vector<S_Props>& 属性) {
	C_工具条* box = new C_工具条(ctx);
	box->m_子组件 = f_ui_从属性构建组件(box, 属性, box->m_布局方式);

	for (auto& e : box->m_子组件) {
		e->m_父部件 = box;
	}

	return box;
}







